<?php
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

require_once __DIR__.'/config.php';

$exchange_name = 'test_exchange';
$routing_key   = 'test_routing_key';
$queue_name    = 'test_queue';

//建立连接
$connection = new AMQPStreamConnection(HOST,PORT,USER,PASS,VHOST);

//创建通道
$channel = $connection->channel();

//声明交换机
$channel->exchange_declare($exchange_name,AMQP_EX_TYPE_TOPIC);

//什么队列
$channel->queue_declare($queue_name,false, true, false, false);

//交换机路由绑定
$channel->queue_bind($queue_name,$exchange_name,$routing_key);

$content = '测试数据加载成功';

//创建消息实例
$message = new AMQPMessage($content);

//消息推送
echo " [x] Sent non-mandatory ...";
$channel->basic_publish($message,$exchange_name,$routing_key,true);
echo " done.\n";

$wait = true;

//监听消息模板，抛出异常
$returnListener = function (
    $replyCode,
    $replyText,
    $exchange,
    $routingKey,
    $message
) use (&$wait){
    $wait = false;

    echo "return:".PHP_EOL,
    "replyCode  :$replyCode".PHP_EOL,
    "replyText  :$replyText".PHP_EOL,
    "exchange   :$exchange".PHP_EOL,
    "routingKey :$routingKey".PHP_EOL,
    "message    :$message->body".PHP_EOL;
};

//设置监听模式
$channel->set_return_listener($returnListener);

//消息循环输出到控制台
while ($wait) $channel->wait();

$channel->close();
$connection->close();
